package com.uinnova.product.eam.web.config.peer.impl;

import com.binary.core.io.FileSystem;
import com.binary.core.io.Resource;
import com.binary.core.io.support.ByteArrayResource;
import com.binary.core.util.BinaryUtils;
import com.binarys.product.rsm.client.RsmClient;
import com.uinnova.product.eam.comm.model.CVcBaseConfig;
import com.uinnova.product.eam.comm.model.VcBaseConfig;
import com.uinnova.product.eam.service.VcBaseConfigSvc;
import com.uinnova.product.eam.web.config.peer.BaseConfigPeer;
import com.uinnova.product.vmdb.comm.i18n.MessageUtil;
import com.uinnova.product.vmdb.comm.i18n.VerifyType;
import com.uinnova.product.vmdb.comm.model.monitor.CMonSysSeverity;
import com.uinnova.product.vmdb.comm.model.monitor.MonSysSeverity;
import com.uino.util.sys.SysUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
//import com.uinnova.product.vmdb.provider.monitor.SysSeveritySvc;

public class BaseConfigPeerImpl implements BaseConfigPeer {

	private final static String appWallOrderConfigFilePathCfgCode = "appWallOrderConfigFilePath";
	
	@Autowired
	private VcBaseConfigSvc configSvc;
	
	private RsmClient rsmClient;
	
	/** RSM-SLAVE访问入口 **/
	private String rsmSlaveRoot;
	
	
	/** RSM分类代码 **/
	private String rsmClassCode;
	
	public String getRsmSlaveRoot() {
		return rsmSlaveRoot;
	}

	public void setRsmSlaveRoot(String rsmSlaveRoot) {
		this.rsmSlaveRoot = rsmSlaveRoot;
	}

	public String getRsmClassCode() {
		return rsmClassCode;
	}

	public void setRsmClassCode(String rsmClassCode) {
		this.rsmClassCode = rsmClassCode;
	}
	
	@Override
	public List<VcBaseConfig> queryConfigList(CVcBaseConfig cdt, String orders) {
		Long domainId = SysUtil.getCurrentUserInfo().getDomainId();
		MessageUtil.checkOrderField(orders);
		if(BinaryUtils.isEmpty(cdt)) cdt = new CVcBaseConfig();
		
		cdt.setDomainId(domainId);
		return configSvc.queryBaseConfigList(domainId, cdt, orders);
	}

	@Override
	public Long saveOrUpdateConfig(VcBaseConfig record) {
		Long domainId = SysUtil.getCurrentUserInfo().getDomainId();
		MessageUtil.checkEmpty(record, "record");
		MessageUtil.checkEmpty(record.getCfgCode(), "cfgCode");
		MessageUtil.checkEmpty(record.getCfgContent(), "cfgContent");
		record.setDomainId(domainId);
		//如果新增则判断cfgCode是否存在
		if(BinaryUtils.isEmpty(record.getId())){
			CVcBaseConfig cdt = new CVcBaseConfig();
			cdt.setCfgCodeEqual(record.getCfgCode());
			cdt.setDomainId(domainId);
			List<VcBaseConfig> list = configSvc.queryBaseConfigList(domainId, cdt, null);
			if(!BinaryUtils.isEmpty(list)) MessageUtil.throwVerify(VerifyType.EXIST, "cfgCode", record.getCfgCode());
		}
		
		return configSvc.saveOrUpdateBaseConfig(domainId, record);
	}
	
	@Override
	public Integer saveOrUpdateConfigBatch(List<VcBaseConfig> records){
		
		MessageUtil.checkEmpty(records, "records");
		for(VcBaseConfig record :records){
			saveOrUpdateConfig(record);
		}
		return 1;
	}
	
	@Override
	public String saveAppWallOrderConfig(String content,String token){
		
		String cfgCode = appWallOrderConfigFilePathCfgCode;
		return saveConfigFileComm(cfgCode, content, token);
	}
	
	@Override
	public String saveOrUpdateConfigFile(String cfgCode, String cfgContent, String token) {
		return saveConfigFileComm(cfgCode, cfgContent, token);
	}
	
	private synchronized String saveConfigFileComm(String cfgCode,String content,String token){
		if(BinaryUtils.isEmpty(cfgCode)) return null;
		Long domainId = SysUtil.getCurrentUserInfo().getDomainId();
		
		CVcBaseConfig cdt = new CVcBaseConfig();
		cdt.setCfgCodeEqual(cfgCode);
		cdt.setDomainId(domainId);
		List<VcBaseConfig> configList = configSvc.queryBaseConfigList(domainId, cdt, null);
		String filePath = null;
		if(!BinaryUtils.isEmpty(configList)){
			String cfgContent = configList.get(0).getCfgContent();
			if(!BinaryUtils.isEmpty(cfgContent)) filePath = cfgContent;
		}
		filePath = replaceSource(content,cfgCode,filePath,token);
		
		if(BinaryUtils.isEmpty(configList)){
			VcBaseConfig record = new VcBaseConfig();
			record.setCfgCode(cfgCode);
			record.setCfgContent(filePath);
			configSvc.saveOrUpdateBaseConfig(domainId, record);
		}else{
			VcBaseConfig vcBaseConfig = configList.get(0);
			if(BinaryUtils.isEmpty(vcBaseConfig.getCfgContent())){
				vcBaseConfig.setCfgContent(filePath);
				configSvc.saveOrUpdateBaseConfig(domainId, vcBaseConfig);
			}
		}
		return filePath;
	}
	
	
	@Override
	public String queryAppWallOrderConfig(String token){
		String cfgCode = appWallOrderConfigFilePathCfgCode;
		
		return queryConfigFileByCode(cfgCode,token,null);
	}
	
	@Override
	public String queryConfigFile(String cfgCode, String token) {
		return queryConfigFile(cfgCode,token,null);
	}
	@Override
	public String queryConfigFile(String cfgCode, String token,Long domainId){
		return queryConfigFileByCode(cfgCode,token,domainId);
	}

	private String queryConfigFileByCode(String cfgCode,String token,Long domainId){
		if(BinaryUtils.isEmpty(cfgCode)) return null;
		if(domainId==null)domainId = SysUtil.getCurrentUserInfo().getDomainId();
		
		CVcBaseConfig cdt = new CVcBaseConfig();
		cdt.setCfgCodeEqual(cfgCode);
		cdt.setDomainId(domainId);
		List<VcBaseConfig> configList = configSvc.queryBaseConfigList(domainId, cdt, null);
		
		String result = "";
		
		if(!BinaryUtils.isEmpty(configList) && !BinaryUtils.isEmpty(configList.get(0).getCfgContent())){
			String filePath = configList.get(0).getCfgContent();
			Resource resource = getResource(filePath, token);
			if(!BinaryUtils.isEmpty(resource)) result = FileSystem.read(resource.getInputStream());
		}
		
		return result;
	}
	
	private Resource getResource(String path,String token){
		if(BinaryUtils.isEmpty(path) || BinaryUtils.isEmpty(token)) return null;
		return rsmClient.getResource(path, token);
	}
	
	private String replaceSource(String content,String name,String path,String token){
		Resource resource = new ByteArrayResource(content.getBytes(), name+".json");
		try {
			if (path == null) {
				path = rsmClient.saveResource(resource, this.rsmClassCode, token);
			} else {
				rsmClient.replaceResource(resource, path, token);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path;
	}

	@Override
	public Integer removeConfigByCdt(CVcBaseConfig cdt) {
		Long domainId = SysUtil.getCurrentUserInfo().getDomainId();
		if(BinaryUtils.isEmpty(cdt)) cdt = new CVcBaseConfig();
		cdt.setDomainId(domainId);
		return configSvc.removeBaseConfigByCdt(domainId, cdt);
	}

}
